package com.dd.ent.car.ui.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.User;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.ui.user.note.UserNoteActivity;
import com.dd.ent.car.ui.user.uservideo.UserVideoActivity;
import com.dd.ent.car.ui.widget.CircleImageView;
import com.duanqu.qupai.utils.ToastUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.pp.ent.base.BaseFragment;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.ContentView;
import com.pp.ent.view.annotation.Inject;
import com.pp.ent.view.annotation.OnClick;
import com.pp.ent.view.annotation.ViewInject;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

/**
 * Created by dengqu on 2016/8/17.
 */
@ContentView(R.layout.message_fragment_layout)
public class UserFragment extends BaseFragment {
    private final static String TAG = "UserFragment";

    @Inject
    private UserService userService;
    @ViewInject(R.id.tv_not_login)
    private TextView tvNotLogin;

    @ViewInject(R.id.thrid_login_view)
    private LinearLayout thridLoginView;

    @ViewInject(R.id.iv_user_icon_no_login)
    private CircleImageView ivUserIcon;

    @ViewInject(R.id.new_user_center_setting)
    private View viewSetting;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MLog.debug(TAG, "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        MLog.debug(TAG, "onCreateView");
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        initData();
        return rootView;
    }

    private void initData() {
        if (userService.isLogin()) {
            tvNotLogin.setVisibility(View.GONE);
            thridLoginView.setVisibility(View.GONE);
            viewSetting.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(userService.getUser().getAvathor(), ivUserIcon);
        } else {
            tvNotLogin.setVisibility(View.VISIBLE);
            viewSetting.setVisibility(View.GONE);
            ivUserIcon.setImageResource(R.drawable.user_not_login_avatar);
        }

    }

    @OnClick({R.id.new_user_center_video, R.id.new_user_center_forum, R.id.new_user_center_forum_draft, R.id.iv_user_icon_no_login, R.id.tv_not_login, R.id.new_user_center_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.new_user_center_video:
                if (userService.isLogin()) {
                    UserVideoActivity.startSelf(getContext());
                } else {
                    LoginActivity.startActivity(getContext());
                }
                break;
            case R.id.new_user_center_forum:
                if (userService.isLogin()) {
                    UserNoteActivity.startSelf(getContext(), UserNoteActivity.Type.NETWORK);
                } else {
                    LoginActivity.startActivity(getContext());
                }
                break;
            case R.id.new_user_center_forum_draft:
                UserNoteActivity.startSelf(getContext(), UserNoteActivity.Type.DRAFT);
                break;
            case R.id.iv_user_icon_no_login:
            case R.id.tv_not_login:
                if (!userService.isLogin()) {
                    LoginActivity.startActivity(getContext());
                } else {
                    EditUserActivity.startActivity(getContext());
                }
                break;
            case R.id.new_user_center_setting:
                SettingActivity.startSelf(getContext());
                break;
        }
    }

    @UIHandler(UIConfig.UPDATE_NAME_SEX_SUCCESS)
    public void updateNameSexResponse() {
        User user = userService.getUser();
        if (user != null) {
            ImageLoader.getInstance().displayImage(user.getAvathor(), ivUserIcon);
        }
    }

    @UIHandler(UIConfig.PHONE_NUMBER_LOGIN_SUCCESS)
    public void phoneNumberRegisteSuccessResponse(User user) {
        initData();
    }

    @UIHandler(UIConfig.NOTIFY_LOGIN_OUT_SUCCESS)
    public void onLoginOutSuccessResponse() {
        initData();
    }
}
